var requireDirectory = require('require-directory'); //自动化引入 router
const Router = require('koa-router')

class InitManager {
    static initCore(app) {

        //入口方法
        this.app = app
        this.initLoadRouters()
        this.loadHttpException()
        InitManager.loadConfig()
        //   // 线上环境
  
  
    }
    static initLoadRouters() {
        //第一个参数是模块 第二个是路径 第三个参数 是一个对象 visit:fubction
        const apiDirectory = `${process.cwd()}/app/api` //绝对路径 
        requireDirectory(module, apiDirectory, { visit: whenLoadModule })
        function whenLoadModule(obj) {
            if (obj instanceof Router) {
                InitManager.app.use(obj.routes())
            }
        }
    }
    static loadConfig(path = '') {
        const configPath = path || process.cwd() + '/config/config.js'
        const config = require(configPath)
        global.config = config
    }

    static loadHttpException() {
        const errors = require('./http-exception')
        global.errs = errors

    }


}
module.exports = InitManager  